package com.jacken.mars.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jacken.mars.commons.constant.CommonConstants;
import com.jacken.mars.commons.exception.WorkException;
import com.jacken.mars.entity.base.BasePageRequest;
import com.jacken.mars.entity.base.PageInfo;
import com.jacken.mars.entity.req.SysNoticeReq;
import com.jacken.mars.entity.vo.SysNoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jacken.mars.service.ISysNoticeService;
import com.jacken.mars.mapper.SysNoticeMapper;
import com.jacken.mars.entity.SysNotice;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * SysNotice 实现层
 *
 * @Author wq
 * @Date 2021-02-06
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {


    @Override
    public void add(SysNoticeReq req) {
        if (StringUtils.isEmpty(req.getNoticeTitle())) {
            throw new WorkException("通知标题不能为空");
        }
        if (Objects.isNull(req.getNoticeType())) {
            throw new WorkException("通知类型不能为空");
        }
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeTitle(req.getNoticeTitle());
        sysNotice.setNoticeType(req.getNoticeType());
        sysNotice.setNoticeContent(req.getNoticeContent());
        sysNotice.setCreateTime(new Date());
        sysNotice.setUpdateTime(new Date());
        sysNotice.setStatus(req.getStatus());
        save(sysNotice);
    }

    @Override
    public PageInfo<SysNoticeVo> sysNoticePage(SysNoticeReq req) {
        IPage<SysNotice> page = new Page<>(req.getPageNumber(), req.getPageSize());
        LambdaQueryWrapper<SysNotice> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(req.getNoticeTitle())) {
            wrapper.like(SysNotice::getNoticeTitle, req.getNoticeTitle());
        }
        if (StringUtils.isNotEmpty(req.getNoticeContent())) {
            wrapper.like(SysNotice::getNoticeContent, req.getNoticeContent());
        }
        if (Objects.nonNull(req.getNoticeType())) {
            wrapper.eq(SysNotice::getNoticeType, req.getNoticeType());
        }
        wrapper.orderByDesc(SysNotice::getCreateTime);
        List<SysNoticeVo> list = baseMapper.selectPage(page, wrapper).getRecords().stream().map(x -> {
            SysNoticeVo vo = new SysNoticeVo();
            vo.setId(x.getId());
            vo.setNoticeContent(x.getNoticeContent());
            vo.setNoticeType(x.getNoticeType());
            vo.setCreateTime(x.getCreateTime());
            vo.setUpdateTime(x.getUpdateTime());
            vo.setNoticeTitle(x.getNoticeTitle());
            vo.setStatus(x.getStatus());
            return vo;
        }).collect(Collectors.toList());
        return new PageInfo<>(page.getTotal(), list);
    }
}